package com.lys.emsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lys.emsystem.entity.Student;
import com.lys.emsystem.entity.Tripartite;
import com.lys.emsystem.service.IStudentService;
import com.lys.emsystem.service.ITripartiteService;
import com.lys.emsystem.utils.JWTUtils;
import com.lys.emsystem.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyansheng
 * @since 2022-05-05
 */
@RestController
@CrossOrigin
@RequestMapping("/tripartite")
public class TripartiteController {
    @Autowired
    private ITripartiteService iTripartiteService;

    @Autowired
    private IStudentService iStudentService;

    /**
     * 获取某个学生的签约信息
     * @param token
     * @return
     */
    @GetMapping
    public Result getTripartiteInfoByToken(@RequestHeader("token")String token) {
        if (StringUtils.isEmpty(token)) {
            return Result.buildResult(Result.Status.BAD_REQUEST, "请先登录！");
        }
        HashMap<String, Object> map = new HashMap<>();
        //    获取学生id
        Integer userId = JWTUtils.getUserId(token);
        //获取学生的签约状态
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);
        Student student = iStudentService.getOne(queryWrapper);
        if (student == null) {
            return Result.buildResult(Result.Status.NOT_FOUND, "没有找到此学生信息");
        }
        if (student.getEmpStatus() == false) {
            map.put("emp_status", student.getEmpStatus());
            return Result.buildResult(Result.Status.NOT_FOUND, "您还未签约公司！",map);
        }
        QueryWrapper<Tripartite> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", userId);
        Tripartite tripartite = iTripartiteService.getOne(wrapper);
        if (tripartite == null) {
            return Result.buildResult(Result.Status.NOT_FOUND, "没有找到相关信息");
        }

        map.put("emp_status", student.getEmpStatus());
        map.put("tripartite", tripartite);
        return Result.buildResult(Result.Status.OK, map);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Tripartite tripartite) {
        System.out.println(tripartite);
        if (iTripartiteService.updateById(tripartite)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR, "更新出错");
        }
    }
}
